package com.sdgakj.utils;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.util.Map;


public class HttpClientUtil {



    private static MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();

    private static final int TIMEOUT = 20 * 1000;

    private static final int MAX_HTTP_CONNECTION = 200;

    private static final int MAX_CONNECTION_PER_HOST = 200;

    private static HttpClientUtil instance = null;



    static {

        // HttpClient 连接池属性设置，HOST并发数默认为50, 客户端总并发数为200, TimeOut时间为5s.

        HttpConnectionManagerParams httpConnectionManagerParams = new HttpConnectionManagerParams();

        httpConnectionManagerParams.setMaxTotalConnections(MAX_HTTP_CONNECTION);

        httpConnectionManagerParams.setDefaultMaxConnectionsPerHost(MAX_CONNECTION_PER_HOST);

        httpConnectionManagerParams.setSoTimeout(TIMEOUT);

        httpConnectionManagerParams.setConnectionTimeout(TIMEOUT);

        connectionManager.setParams(httpConnectionManagerParams);

    }

    public HttpClientUtil() {



    }



    public static HttpClientUtil getInstance() {



        if (null == instance) {

            synchronized (HttpClientUtil.class) {



                if (instance == null) {

                    instance = new HttpClientUtil();

}

            }

        }

        return instance;

    }



    public static HttpClient createHttpClient() {



        HttpClient httpClient = new HttpClient(connectionManager);



        return httpClient;

    }

    /**
     * 
     * 进行 http请求返回resultDto结果对象
     * 
     * 
     * 
     ** @param //url
     * 
     * @return
     * 
     */

    public static String getHttpClientJson(GetMethod getMethod) {



        HttpClient httpClient = createHttpClient();

        String jsonString = "";

        try {

            httpClient.executeMethod(getMethod);
            jsonString = getMethod.getResponseBodyAsString();
        } catch (Exception e) {

            e.printStackTrace();


        } finally {

            getMethod.releaseConnection();

        }
        return jsonString;

    }



    /**
     * 
     * 进行 http post请求返回resultDto结果对象
     * 
     * 
     * 
     * @param  //url
     * 
     * @return
     * 
     */

    public static String postHttpClientJson(PostMethod postMethod) {



        HttpClient httpClient = createHttpClient();
        httpClient.getParams().setContentCharset("UTF-8");
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(6000);
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(6000);
        String jsonString = "";
        try {

            httpClient.executeMethod(postMethod);

            jsonString = postMethod.getResponseBodyAsString();

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            postMethod.releaseConnection();

        }
        return jsonString;
    }

    public static String doGet(String url, Map<String, String> param) {

        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();

        String resultString = "";
        CloseableHttpResponse response = null;
        try {
            // 创建uri
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
                for (String key : param.keySet()) {
                    builder.addParameter(key, param.get(key));
                }
            }
            URI uri = builder.build();
            // 创建http GET请求
            HttpGet httpGet = new HttpGet(uri);
            // 执行请求
            response = httpclient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultString;
    }

}

